# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=fonttools
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}")
pkgver=3.44.0
pkgrel=1
pkgdesc="Converts OpenType and TrueType fonts to and from XML (mingw-w64)"
arch=('any')
url='https://github.com/fonttools/fonttools'
license=('MIT')
depends=("${MINGW_PACKAGE_PREFIX}-python2"
         "${MINGW_PACKAGE_PREFIX}-python2-numpy")
optdepends=("${MINGW_PACKAGE_PREFIX}-python2-brotli: WOFF 1.0 and WOFF 2.0 webfonts"
            "${MINGW_PACKAGE_PREFIX}-python2-pyfilesystem2: to read/write UFO fonts"
            "${MINGW_PACKAGE_PREFIX}-python2-enum34: to read/write UFO fonts"
            "${MINGW_PACKAGE_PREFIX}-python2-pyzopfli: WOFF 1.0 and WOFF 2.0 webfonts"
            "${MINGW_PACKAGE_PREFIX}-python2-sympy: for fontTools.misc.symfont, module for symbolic font statistics analysis"
            "${MINGW_PACKAGE_PREFIX}-python2-unicodedata2: for Unicode 12.0")
makedepends=("${MINGW_PACKAGE_PREFIX}-python2-matplotlib"
             "${MINGW_PACKAGE_PREFIX}-python2-setuptools")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python2-pytest-runner")
install=${_realname}2-${CARCH}.install
options=('staticlibs' 'strip' '!debug')
source=("https://github.com/fonttools/fonttools/releases/download/${pkgver}/fonttools-${pkgver}.zip")
sha256sums=('b72cc654e07219d8d7d8c624d1af13cec6f6cc2a51189f8385c295725e4ac36c')

build() {
  msg "Python 2 build for ${CARCH}"
  rm -rf python2-build-${CARCH}
  cp -r "${_realname}-${pkgver}" "python2-build-${CARCH}"
  cd "${srcdir}/python2-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python${pver} setup.py build
}

#Enable tests when the dependencies are solved
check() {
  msg "Python 2 test for ${CARCH}"
  cd "${srcdir}/python2-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python2 setup.py pytest || warning "Tests failed"
}

package() {
  cd "${srcdir}/python2-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _ff in ${pkgdir}${MINGW_PREFIX}/bin/*.py; do
    sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_ff}
  done

# for Python2 packages, you want to rename some stuff from the bin directory 
# with the 2 suffix like in the mingw-w64-python-pygments package to avoid
# conflicts when installing both the Python2 and Python3 packages
  for f in fonttools; do
    mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}.exe
    if [ -f "${pkgdir}${MINGW_PREFIX}"/bin/${f}.exe.manifest ]; then
      mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}.exe.manifest
      sed -e "s|${f}|${f}2|g" -i "${pkgdir}${MINGW_PREFIX}"/bin/${f}2.exe.manifest
    fi
    mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}-script.py
  done

  for f in pyftmerge; do
    mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}.exe
    if [ -f "${pkgdir}${MINGW_PREFIX}"/bin/${f}.exe.manifest ]; then
      mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}.exe.manifest
      sed -e "s|${f}|${f}2|g" -i "${pkgdir}${MINGW_PREFIX}"/bin/${f}2.exe.manifest
    fi
    mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}-script.py
  done

  for f in pyftsubset; do
    mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}.exe
    if [ -f "${pkgdir}${MINGW_PREFIX}"/bin/${f}.exe.manifest ]; then
      mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}.exe.manifest
      sed -e "s|${f}|${f}2|g" -i "${pkgdir}${MINGW_PREFIX}"/bin/${f}2.exe.manifest
    fi
    mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}-script.py
  done

  for f in ttx; do
    mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}.exe
    if [ -f "${pkgdir}${MINGW_PREFIX}"/bin/${f}.exe.manifest ]; then
      mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}.exe.manifest
      sed -e "s|${f}|${f}2|g" -i "${pkgdir}${MINGW_PREFIX}"/bin/${f}2.exe.manifest
    fi
    mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}-script.py
    mv "${pkgdir}${MINGW_PREFIX}"/share/man/man1/${f}{,2}.1
  done

#### end section ####
  install -D -m755 LICENSE "${pkgdir}${MINGW_PREFIX}"/share/licenses/python2-${_realname}/LICENSE
  chmod oga+r "${pkgdir}${MINGW_PREFIX}"/share/man/man1/ttx2.1
}
